package notifications

import (
	"gpt-api/framework/codes"
	"gpt-api/framework/db"
	"gpt-api/framework/ginplus"
	"gpt-api/pkg/model"

	"github.com/gin-gonic/gin"
)

type Handler struct {
	db *db.DB
}

// 通知列表响应结构体
type NotificationsListResp struct {
	ID        uint64 `json:"id"`
	Title     string `json:"title"`
	Content   string `json:"content"`
	CreatedAt string `json:"created_at"`
	Status    int    `json:"status"`
}

// 通知详情响应结构体
type NotificationsInfoResp struct {
	ID        uint64 `json:"id"`
	Title     string `json:"title"`
	Content   string `json:"content"`
	CreatedAt string `json:"created_at"`
	Status    int    `json:"status"`
}

func New(db *db.DB) *Handler {
	return &Handler{db: db}
}

// NotificationsList 返回用户的通知列表
func (h *Handler) NotificationsList(c *gin.Context) error {
	userId := ginplus.GetUserID(c)
	notifications, err := (&model.Notifications{}).GetNotificationsByUserId(h.db, userId)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	// 遍历通知列表
	respList := make([]*NotificationsListResp, 0)
	for _, notification := range notifications {
		respList = append(respList, &NotificationsListResp{
			ID:        notification.ID,
			Title:     notification.Title,
			Content:   notification.Content,
			CreatedAt: notification.CreatedAt.Format("2006-01-02 15:04:05"),
			Status:    notification.Status,
		})
	}

	return ginplus.ReplyJSON(c, respList)
}

// NotificationsList 返回用户的通知详情
func (h *Handler) NotificationsInfo(c *gin.Context) error {
	// 验证参数
	var req struct {
		ID uint64 `form:"id" binding:"required,gte=0"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 查询通知
	notificationsInfo, err := (&model.Notifications{}).GetNotificationsById(h.db, req.ID)
	if err != nil {
		return ginplus.ReplyError(c, codes.NotificationNotFound, err)
	}

	// 返回响应
	info := &NotificationsInfoResp{
		ID:        notificationsInfo.ID,
		Title:     notificationsInfo.Title,
		Content:   notificationsInfo.Content,
		CreatedAt: notificationsInfo.CreatedAt.Format("2006-01-02 15:04:05"),
		Status:    notificationsInfo.Status,
	}

	return ginplus.ReplyJSON(c, info)
}

// MarkAllNotificationsRead 标记所有通知为已读
func (h *Handler) MarkAllNotificationsRead(c *gin.Context) error {
	userId := ginplus.GetUserID(c)

	// 更新数据
	err := (&model.Notifications{}).MarkAllNotificationsRead(h.db, userId)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	return ginplus.ReplyJSON(c, nil)
}

// MarkNotificationsReadId 标记指定通知为已读
func (h *Handler) MarkNotificationsReadId(c *gin.Context) error {
	// 验证参数
	var req struct {
		ID uint64 `json:"id" binding:"required,gte=0"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 更新数据
	userId := ginplus.GetUserID(c)
	err := (&model.Notifications{}).MarkNotificationRead(h.db, req.ID, userId)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	return ginplus.ReplyJSON(c, nil)
}

// DeleteNotification 删除指定的通知
func (h *Handler) DeleteNotification(c *gin.Context) error {
	var req struct {
		ID uint64 `json:"id" binding:"required"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	userId := ginplus.GetUserID(c)
	err := (&model.Notifications{}).DeleteNotification(h.db, req.ID, userId)
	if err != nil {
		return ginplus.ReplyError(c, codes.NotificationNotFound, err)
	}
	return ginplus.ReplyJSON(c, nil)
}
